home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / PROGRAMMING / PERL.SPK / Perl5001 / Manual / perlsec_ht < prev    next >
Text File  |  1995-04-18  |  7KB  |  126 lines

  1. <!-- $RCSfile$$Revision$$Date$ -->
  2. <!-- $Log$ -->
  3. <HTML>
  4. <TITLE> PERLSEC </TITLE>
  5. <h2>NAME</h2>
  6. perlsec - Perl security
  7. <p><h2>DESCRIPTION</h2>
  8. Perl is designed to make it easy to write secure setuid and setgid
  9. scripts.  Unlike shells, which are based on multiple substitution
  10. passes on each line of the script, Perl uses a more conventional
  11. evaluation scheme with fewer hidden "gotchas".  Additionally, since the
  12. language has more built-in functionality, it has to rely less upon
  13. external (and possibly untrustworthy) programs to accomplish its
  14. purposes.
  15. <p>Beyond the obvious problems that stem from giving special privileges to
  16. such flexible systems as scripts, on many operating systems, setuid
  17. scripts are inherently insecure right from the start.  This is because
  18. that between the time that the kernel opens up the file to see what to
  19. run, and when the now setuid interpreter it ran turns around and reopens
  20. the file so it can interpret it, things may have changed, especially if
  21. you have symbolic links on your system.
  22. <p>Fortunately, sometimes this kernel "feature" can be disabled.
  23. Unfortunately, there are two ways to disable it.  The system can simply
  24. outlaw scripts with the setuid bit set, which doesn't help much.
  25. Alternately, it can simply ignore the setuid bit on scripts.  If the
  26. latter is true, Perl can emulate the setuid and setgid mechanism when it
  27. notices the otherwise useless setuid/gid bits on Perl scripts.  It does
  28. this via a special executable called <B>suidperl</B> that is automatically
  29. invoked for you if it's needed.
  30. <p>If, however, the kernel setuid script feature isn't disabled, Perl will
  31. complain loudly that your setuid script is insecure.  You'll need to
  32. either disable the kernel setuid script feature, or put a C wrapper around
  33. the script.  See the program <B>wrapsuid</B> in the <I>eg</I> directory of your
  34. Perl distribution for how to go about doing this.
  35. <p>There are some systems on which setuid scripts are free of this inherent
  36. security bug.  For example, recent releases of Solaris are like this.  On
  37. such systems, when the kernel passes the name of the setuid script to open
  38. to the interpreter, rather than using a pathname subject to mettling, it
  39. instead passes /dev/fd/3.  This is a special file already opened on the
  40. script, so that there can be no race condition for evil scripts to
  41. exploit.  On these systems, Perl should be compiled with
  42. <B>-DSETUID_SCRIPTS_ARE_SECURE_NOW</B>.  The <B>Configure</B> program that builds
  43. Perl tries to figure this out for itself.
  44. <p>When Perl is executing a setuid script, it takes special precautions to
  45. prevent you from falling into any obvious traps.  (In some ways, a Perl
  46. script is more secure than the corresponding C program.)  Any command line
  47. argument, environment variable, or input is marked as "tainted", and may
  48. not be used, directly or indirectly, in any command that invokes a
  49. subshell, or in any command that modifies files, directories, or
  50. processes.  Any variable that is set within an expression that has
  51. previously referenced a tainted value also becomes tainted (even if it is
  52. logically impossible for the tainted value to influence the variable).
  53. For example:
  54. <p><pre>
  55.         $foo = shift;           # $foo is tainted
  56.         $bar = $foo,'bar';              # $bar is also tainted
  57.         $xxx = <>;                        # Tainted
  58.         $path = $ENV{'PATH'};   # Tainted, but see below
  59.         $abc = 'abc';           # Not tainted
  60. </pre>
  61. <pre>
  62.         system "echo $foo";             # Insecure
  63.         system "/bin/echo", $foo;       # Secure (doesn't use sh)
  64.         system "echo $bar";             # Insecure
  65.         system "echo $abc";             # Insecure until PATH set
  66. </pre>
  67. <pre>
  68.         $ENV{'PATH'} = '/bin:/usr/bin';
  69.         $ENV{'IFS'} = '' if $ENV{'IFS'} ne '';
  70. </pre>
  71. <pre>
  72.         $path = $ENV{'PATH'};   # Not tainted
  73.         system "echo $abc";             # Is secure now!
  74. </pre>
  75. <pre>
  76.         open(FOO,"$foo");               # OK
  77.         open(FOO,">$foo");              # Not OK
  78. </pre>
  79. <pre>
  80.         open(FOO,"echo $foo|"); # Not OK, but...
  81.         open(FOO,"-|") || exec 'echo', $foo;    # OK
  82. </pre>
  83. <pre>
  84.         $zzz = `echo $foo`;             # Insecure, zzz tainted
  85. </pre>
  86. <pre>
  87.         unlink $abc,$foo;               # Insecure
  88.         umask $foo;                     # Insecure
  89. </pre>
  90. <pre>
  91.         exec "echo $foo";               # Insecure
  92.         exec "echo", $foo;              # Secure (doesn't use sh)
  93.         exec "sh", '-c', $foo;  # Considered secure, alas
  94. </pre>
  95. The taintedness is associated with each scalar value, so some elements
  96. of an array can be tainted, and others not.
  97. <p>If you try to do something insecure, you will get a fatal error saying
  98. something like "Insecure dependency" or "Insecure PATH".  Note that you
  99. can still write an insecure system call or exec, but only by explicitly
  100. doing something like the last example above.  You can also bypass the
  101. tainting mechanism by referencing subpatterns--Perl presumes that if
  102. you reference a substring using $1, $2, etc, you knew what you were
  103. doing when you wrote the pattern:
  104. <p><pre>
  105.         $ARGV[0] =~ /^-P(\w+)$/;
  106.         $printer = $1;          # Not tainted
  107. </pre>
  108. This is fairly secure since <B>\w+</B> doesn't match shell metacharacters.
  109. Use of <B>/.+/</B> would have been insecure, but Perl doesn't check for that,
  110. so you must be careful with your patterns.  This is the <I>ONLY</I> mechanism
  111. for untainting user supplied filenames if you want to do file operations
  112. on them (unless you make <B>$></B> equal to <B>$<</B> ).
  113. <p>For "Insecure PATH" messages, you need to set <B>$ENV{'PATH}'</B> to a known
  114. value, and each directory in the path must be non-writable by the world.
  115. A frequently voiced gripe is that you can get this message even
  116. if the pathname to an executable is fully qualified.  But Perl can't
  117. know that the executable in question isn't going to execute some other
  118. program depending on the PATH.
  119. <p>It's also possible to get into trouble with other operations that don't
  120. care whether they use tainted values.  Make judicious use of the file
  121. tests in dealing with any user-supplied filenames.  When possible, do
  122. opens and such after setting <B>$> = $<</B>.  (Remember group IDs,
  123. too!) Perl doesn't prevent you from opening tainted filenames for reading,
  124. so be careful what you print out.  The tainting mechanism is intended to
  125. prevent stupid mistakes, not to remove the need for thought.
  126. <p>